cc.Class({
  extends: cc.Component,
  properties: {
    speed: 0.2,
  },
  ctor() {
    this.inAnim = false;
    this.currentAnimValue = 0;
    this.progress = 0;
    this.animTo = null;
  },
  
  onLoad() {
    this.progressBar = this.node.getComponent(cc.ProgressBar);
  },
  
  start() {
    this._setProgressBar(0);
  },
  
  _setProgressBar(progress) {
    if (this.progressBar) {
      this.progressBar.progress = progress;
    }
  },
  
  setProgress(progress) {
    this.progress = progress;
  },
  
  setAnimationTo(progress) {
    this.animTo = progress;
    if (this.inAnim) {
      this.animFrom = this.currentAnimValue;
    } else {
      this.animFrom = this.progress;
    }
    this.inAnim = true;
    this.animDt = 0;
    this.progress = progress;
  },
  
  getProgress() {
    return this.progress;
  },
  
  update(dt) {
    if (this.progressBar) {
      if (this.inAnim) {
        this.animDt += dt;
        if (this.animDt > this.speed) {
          this.animDt = this.speed;
          this.inAnim = false;
        }
        let rate = this.animDt / this.speed;
        this.currentAnimValue = (this.animFrom + (this.animTo - this.animFrom) * rate);
        this._setProgressBar(this.currentAnimValue);
      } else {
        this._setProgressBar(this.progress);
      }
    }
  },
  
});
